package com.heima.freemarker.controller;

import com.heima.freemarker.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller//不可以直接写@RestController，因为里面包含@ResponseBody
@RequestMapping("/hello")
public class HelloController {
    /**
     * 快速入门
     * @return springmvc天然支持freemarker，所以传参数的时候是需要用
     *          model接口
     *          modelMap
     *          ModelAndView
     */
    @GetMapping
    public String index(Model model){
        //1.传参数
        model.addAttribute("name","二狗子");

        Student student=new Student();
        student.setName("张三");
        student.setAge(123);
        model.addAttribute("stu",student);

        //2.跳转页面，只需要名称，不需要携带后缀名
        return "01-basic";
    }

    /**
     * 利用modelMap方式进行传参
     * @param modelMap 作为一个linkedHashMap的子类
     * @return
     */
    @GetMapping("/modelMap")
    public String index2(ModelMap modelMap){
        //1.传参数
        modelMap.put("name","二狗子2222");

        Student student=new Student();
        student.setName("张三");
        student.setAge(123);
        modelMap.put("stu",student);

        //2.跳转页面，只需要名称，不需要携带后缀名
        return "01-basic";
    }

    /**
     * modelAndView模型与视图的类
     * @param
     * @return
     */
    @GetMapping("/mav")
    public ModelAndView index3(){
        ModelAndView mav=new ModelAndView();
        //1.传参数
        mav.addObject("name","二狗子2222");

        Student student=new Student();
        student.setName("张三");
        student.setAge(123);
        mav.addObject("stu",student);

        //2.跳转页面，只需要名称，不需要携带后缀名
        mav.setViewName("01-basic");
        return mav;
    }

    /**
     * 展示list
     */
    @GetMapping("/list")
    public String list(Model model){
    //list遍历
        Student student=new Student();
        student.setName("张三");
        student.setAge(123);
        student.setMoney(100);

        Student student2=new Student();
        student2.setName("李四");
        student2.setAge(12);
        student2.setMoney(80);

        List<Student>list=new ArrayList();
        list.add(student);
        list.add(student2);

        model.addAttribute("stus",list);

   //map遍历
        Map map=new HashMap<>();
        map.put("stu1",student);
        map.put("stu2",student2);
        model.addAttribute("stuMap",map);




        return "02-list";
    }


}
